package com.yoomiito.app.ui.my.angle

import android.content.Context
import android.content.Intent
import android.graphics.PointF
import android.net.Uri
import android.os.Bundle
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.ImageViewState
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.EventMessage
import kotlinx.android.synthetic.main.act_sign_agreement.*
import kotlinx.android.synthetic.main.title_bar.*
import java.io.File

class SignAgreementActivity : BaseActivity<SignAgreementPresenter>() {
    companion object {
        fun start(context: Context) {
            val intent = Intent(context, SignAgreementActivity::class.java)
            context.startActivity(intent)
        }
    }
    private lateinit var url:String
    override fun getLayoutId(): Int = R.layout.act_sign_agreement

    override fun newP(): SignAgreementPresenter = SignAgreementPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        tv_center.text = "签署合同"
        iv_back_left.setOnClickListener { finish() }
//        writeName.setBackColor(Color.WHITE)
//        writeName.setPaintWidth(10)
//        writeName.setPenColor(Color.BLACK)
        agreementIv.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM)
        agreementIv.minScale = 1.0f
        statusTv.setOnClickListener {
            SignActivity.start(this,url)
        }
        showLoading()
        p.agreement()
    }

    fun showAgreement(url:String?){
        this.url = url!!
        Glide.with(this).load(url).downloadOnly(object : SimpleTarget<File>() {
            override fun onResourceReady(resource: File, transition: Transition<in File>?) {
                dismissLoading()
                agreementIv.setImage(ImageSource.uri(Uri.fromFile(resource)), ImageViewState(1.0f, PointF(0f, 0f), 0))
            }
        })
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)
        if (AllCode.ANGLE_AGREEMENT_SIGN == eventMessage?.eventType){
            finish()
        }
    }
}